package com.example.pan.http

import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class SettingsRepository @Inject constructor(private val dataStore: DataStore<Preferences>) {

    companion object {
        val THEME_MODE = intPreferencesKey("themeMode")
    }

    val themeMode: Flow<Int> = dataStore.data.map { preferences ->
        preferences[THEME_MODE] ?: MODE_NIGHT_FOLLOW_SYSTEM
    }

    suspend fun setThemeMode(themeMode: Int) {
        dataStore.edit {
            it[THEME_MODE] = themeMode
            AppCompatDelegate.setDefaultNightMode(themeMode)
        }
    }

}